package ${rootPkg}.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import ${rootPkg}.entity.${bClassName};
import ${rootPkg}.mapper.${bClassName}Mapper;
import ${rootPkg}.service.I${bClassName}Service;
import com.unbox.common.table.UnBoxPage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * <p>
 * <${tableComment}业务实现类
 * </p>
 *
 * @author 王振宇
 * @since ${.now?string('yyyy-MM-dd')}
 */
@Service
public class ${bClassName}ServiceImpl extends ServiceImpl<${bClassName}Mapper, ${bClassName}> implements I${bClassName}Service {

    @Override
    public ${bClassName} get(${bClassName} ${sClassName}) {
        LambdaQueryWrapper<${bClassName}> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        <#list fields as field>
        <#if field.queryType == 'eq'>
            <#if field.javaType == 'String'>
        if (!StringUtils.isEmpty(${sClassName}.${field.javaEntityFieldGetName}())) {
            </#if>
            <#if field.javaType != 'String'>
        if (!ObjectUtils.isEmpty(${sClassName}.${field.javaEntityFieldGetName}())) {
            </#if>
            lambdaQueryWrapper.eq(${bClassName}::${field.javaEntityFieldGetName}, ${sClassName}.${field.javaEntityFieldGetName}());
        }
        </#if>
        <#if field.queryType == 'like'>
            <#if field.javaType == 'String'>
        if (!StringUtils.isEmpty(${sClassName}.${field.javaEntityFieldGetName}())) {
            </#if>
            <#if field.javaType != 'String'>
        if (!ObjectUtils.isEmpty(${sClassName}.${field.javaEntityFieldGetName}())) {
            </#if>
            lambdaQueryWrapper.eq(${bClassName}::${field.javaEntityFieldGetName}, ${sClassName}.${field.javaEntityFieldGetName}());
        }
        </#if>
        </#list>
       return super.getOne(lambdaQueryWrapper);
    }

    @Override
    public List<${bClassName}> getList(${bClassName} ${sClassName}) {
        LambdaQueryWrapper<${bClassName}> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        <#list fields as field>
            <#if field.queryType == 'eq'>
                <#if field.javaType == 'String'>
        if (!StringUtils.isEmpty(${sClassName}.${field.javaEntityFieldGetName}())) {
                </#if>
                <#if field.javaType != 'String'>
        if (!ObjectUtils.isEmpty(${sClassName}.${field.javaEntityFieldGetName}())) {
                </#if>
            lambdaQueryWrapper.eq(${bClassName}::${field.javaEntityFieldGetName}, ${sClassName}.${field.javaEntityFieldGetName}());
        }
            </#if>
            <#if field.queryType == 'like'>
                <#if field.javaType == 'String'>
        if (!StringUtils.isEmpty(${sClassName}.${field.javaEntityFieldGetName}())) {
                </#if>
                <#if field.javaType != 'String'>
        if (!ObjectUtils.isEmpty(${sClassName}.${field.javaEntityFieldGetName}())) {
                </#if>
            lambdaQueryWrapper.like(${bClassName}::${field.javaEntityFieldGetName}, ${sClassName}.${field.javaEntityFieldGetName}());
        }
            </#if>
        </#list>
        return super.list(lambdaQueryWrapper);
    }

    @Override
    public UnBoxPage<List<${bClassName}>> getPageList(${bClassName} ${sClassName}, int pageNo, int pageSize) {
        LambdaQueryWrapper<${bClassName}> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        <#list fields as field>
            <#if field.queryType == 'eq'>
                <#if field.javaType == 'String'>
        if (!StringUtils.isEmpty(${sClassName}.${field.javaEntityFieldGetName}())) {
                </#if>
                <#if field.javaType != 'String'>
        if (!ObjectUtils.isEmpty(${sClassName}.${field.javaEntityFieldGetName}())) {
                </#if>
            lambdaQueryWrapper.eq(${bClassName}::${field.javaEntityFieldGetName}, ${sClassName}.${field.javaEntityFieldGetName}());
        }    </#if>
            <#if field.queryType == 'like'>
                <#if field.javaType == 'String'>
        if (!StringUtils.isEmpty(${sClassName}.${field.javaEntityFieldGetName}())) {
                </#if>
                <#if field.javaType != 'String'>
        if (!ObjectUtils.isEmpty(${sClassName}.${field.javaEntityFieldGetName}())) {
                </#if>
            lambdaQueryWrapper.like(${bClassName}::${field.javaEntityFieldGetName}, ${sClassName}.${field.javaEntityFieldGetName}());
        }    </#if>
        </#list>
         <#list fields as field>
            <#if field.sortType == 'asc'>
        lambdaQueryWrapper.orderByAsc(${bClassName}::${field.javaEntityFieldGetName});
            </#if>
            <#if field.sortType == 'desc'>
        lambdaQueryWrapper.orderByDesc(${bClassName}::${field.javaEntityFieldGetName});
            </#if>
        </#list>
        Page<${bClassName}> page = new Page<>(pageNo,pageSize);
        IPage<${bClassName}> pageResult = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        UnBoxPage<List<${bClassName}>> unBoxPage = new UnBoxPage<>();
        unBoxPage.setPageNo(pageNo);
        unBoxPage.setPageSize(pageSize);
        unBoxPage.setSumPageCount(pageResult.getPages());
        unBoxPage.setSumDataCount(pageResult.getTotal());
        unBoxPage.setDatas(pageResult.getRecords());
        return unBoxPage;
    }

    @Override
    public List<${bClassName}> getIdsList(List<Integer> ids) {
        LambdaQueryWrapper<${bClassName}> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        <#list fields as field>
        <#if field.javaIsPri == '1'>
        lambdaQueryWrapper.in(${bClassName}::${field.javaEntityFieldGetName}, ids);
        </#if>
        </#list>
        return super.list(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean save(${bClassName} entity) {
        return super.save(entity);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveBatch(List<${bClassName}> entityList) {
        return super.saveBatch(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean update(${bClassName} ${sClassName}) {
        LambdaQueryWrapper<${bClassName}> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        <#list fields as field>
            <#if field.javaIsPri == '1'>
        lambdaQueryWrapper.eq(${bClassName}::${field.javaEntityFieldGetName}, ${sClassName}.${field.javaEntityFieldGetName}());
            </#if>
        </#list>
        return super.update(${sClassName}, lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateBatchById(List<${bClassName}> entityList) {
        return super.updateBatchById(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean remove(${bClassName} ${sClassName}) {
        LambdaQueryWrapper<${bClassName}> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        <#list fields as field>
            <#if field.javaIsPri == '1'>
        lambdaQueryWrapper.eq(${bClassName}::${field.javaEntityFieldGetName}, ${sClassName}.${field.javaEntityFieldGetName}());
            </#if>
        </#list>
        return super.remove(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean removeByIds(List<Integer> idList) {
        return super.removeByIds(idList);
    }
}
